package cn.com.lookang.robot.listener;

import cn.com.lookang.robot.service.GroupMessageService;
import cn.hutool.core.util.RandomUtil;
import com.forte.qqrobot.anno.Filter;
import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.beans.messages.msgget.GroupMsg;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import com.forte.qqrobot.beans.types.KeywordMatchType;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;
import com.google.common.collect.Maps;
import net.mamoe.mirai.message.data.At;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author lookang
 * @date 2020/7/23
 */
@Component
@Beans
public class GroupMessageListener {

    @Autowired
    private GroupMessageService groupMessageService;

    private Map<String,Boolean> flagMap = Maps.newConcurrentMap();


    /**
     * 开团:副本名称，开团时间，需求人数
     *
     * @param groupMsg
     * @param sender
     */
    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^[.]*开团:[\\u4e00-\\u9fa5]+,[\\u4e00-\\u9fa5]+,[1-9]+$")
    public void groupPlayRealmListener(GroupMsg groupMsg, MsgSender sender) {
        String msg = groupMsg.getMsg();
        System.out.println("groupPlayRealmListener msg :" + msg);
        String[] info = msg.split("开团:");
        String playerName = groupMsg.getNickname();
        String playerQQ = groupMsg.getQQ();
        String[] split1 = StringUtils.split(info[1], ",");
        if (split1.length!=3) {
            sender.SENDER.sendGroupMsg(groupMsg.getGroup(), "申请开团信息格式为：开团:副本名称,开车时间,需求人数");
        }
        String realmName = split1[0];
        String playDate = split1[1];
        String playerNum = split1[2];
        groupMessageService.playRealm(realmName,playerName,playerQQ,playDate,playerNum);
        sender.SENDER.sendGroupMsg(groupMsg.getGroup(), String.format("@%s  %s开团成功,时间:%s,需求%s人,当前有%s人",playerName, realmName, playDate, playerNum, "1"));
    }


    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^.*报名:[\\u4e00-\\u9fa5]+,[\\u4e00-\\u9fa5]+$", at = true)
    public void enrollRealmByRealmName(GroupMsg groupMsg, MsgSender sender) throws Exception {
        String msg = groupMsg.getMsg();
        System.out.println("enrollRealmByRealmName msg :" + msg);
        String[] info = msg.split("报名:");
        String playerName = groupMsg.getNickname();
        String playerQQ = groupMsg.getQQ();
        String[] split1 = StringUtils.split(info[1], ",");
        String realmName = split1[0];
        String playerCareer = split1[1];
        String returnMsg = groupMessageService.enrollRealm(realmName, playerName, playerQQ, playerCareer);
        sender.SENDER.sendGroupMsg(groupMsg.getGroup(), String.format("@%s  %s",playerName,returnMsg));
    }

    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^.*报名:[\\u4e00-\\u9fa5]+$", at = true)
    public void enrollRealm(GroupMsg groupMsg, MsgSender sender) throws Exception {
        String msg = groupMsg.getMsg();
        System.out.println("enrollRealm msg :" + msg);
        String[] info = msg.split("报名:");
        String playerName = groupMsg.getNickname();
        String playerQQ = groupMsg.getQQ();
        String[] split1 = StringUtils.split(info[1], ",");
        String playerCareer = split1[0];
        String returnMsg = groupMessageService.enrollRealm(null, playerName, playerQQ, playerCareer);
        sender.SENDER.sendGroupMsg(groupMsg.getGroup(), String.format("@%s  %s",playerName,returnMsg));
    }

    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^.*复读机$", at = true)
    public void sitmanStart(GroupMsg groupMsg, MsgSender sender) throws Exception {
        flagMap.put(groupMsg.getGroupCode(),true);
        sender.SENDER.sendGroupMsg(groupMsg.getGroup(), "没有感情的复读机开始工作了");
    }


    @Listen(MsgGetTypes.groupMsg)
    @Filter
    public void sitman(GroupMsg groupMsg, MsgSender sender) throws Exception {
        Boolean aBoolean = flagMap.get(groupMsg.getGroupCode());
        if (aBoolean != null && aBoolean) {
            int i = RandomUtil.randomInt(10);
//            if (i>) {
                String msg = groupMsg.getMsg();
                sender.SENDER.sendGroupMsg(groupMsg.getGroup(), msg);
//            }
        }
    }

    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^.*闭嘴$", at = true)
    public void sitmanStop(GroupMsg groupMsg, MsgSender sender) throws Exception {
        flagMap.put(groupMsg.getGroupCode(),false);
        sender.SENDER.sendGroupMsg(groupMsg.getGroup(), "QAQ,闭嘴就闭嘴！凶什么凶");
    }

}
